package com.ruoyi.syrw.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.syrw.domain.bo.PhbComputeBo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Excel计算类
 *
 * @author zyb
 * @date 2024-04-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/syrw/compute")
public class ExcelComputerController extends BaseController {

    /**
     * 根据配合比和试样升数，计算各用料kg数
     */
    @SaCheckLogin
    @PostMapping("/phb/computeKgNums")
    public R<List<PhbComputeBo>> computeKgNums(@RequestBody List<PhbComputeBo> boList) {
        if (CollUtil.isEmpty(boList)) {
            return R.ok(boList);
        }
        for (PhbComputeBo bo : boList) {
            List<BigDecimal> computeResult = new ArrayList<>();
            bo.setComputeResult(computeResult);
            if (CollUtil.isEmpty(bo.getPhb())) {
                continue;
            }
            if (ObjUtil.isNull(bo.getSum())) {
                continue;
            }
            if (bo.getPhb().contains(null)) {
                continue;
            }
            BigDecimal phbSum = bo.getPhb().stream().reduce(BigDecimal.ZERO, BigDecimal::add);
            for (BigDecimal phb : bo.getPhb()) {
//                computeResult.add(BigDecimalUtil.divide(phb.multiply(bo.getSum()).multiply(bo.getZhuanHuanLv()), phbSum));
                computeResult.add(BigDecimalUtil.divide(BigDecimalUtil.multiply(phb, bo.getSum()), new BigDecimal(1000)));
            }
            bo.setComputeResult(computeResult);
        }
        return R.ok(boList);
    }
}
